package com.wangxinenpu.springbootdemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wangxinenpu.springbootdemo.dao.mapper.OrganizationMapper;
import com.wangxinenpu.springbootdemo.dataobject.dto.organization.OrganizationDto;
import com.wangxinenpu.springbootdemo.dataobject.model.Organization;
import com.wangxinenpu.springbootdemo.service.OrganizationService;
import lombok.Setter;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 组织表 服务实现类
 * </p>
 *
 * @author =
 * @since 2024-06-14
 */
@Service
@Setter ( onMethod_ = @Resource )
public class OrganizationServiceImpl extends ServiceImpl < OrganizationMapper,Organization > implements OrganizationService {

	@Override
	public R < Page < Organization > > pageList( OrganizationDto dto ) {

		Page < Organization > page = this.page( new Page <>( dto.getPageNum() , dto.getPageSize() ) ,
				new LambdaQueryWrapper < Organization >().like( org.apache.commons.lang3.StringUtils.isNotBlank( dto.getOrgName() ) , Organization::getOrgName , dto.getOrgName() ) );
		return R.ok( page );
	}

	@Override
	@Transactional ( rollbackFor = Exception.class )
	public R < Boolean > delete( List < String > ids ) {
		if ( CollectionUtils.isEmpty( ids ) ) return R.failed( "所选删除内容为空" );
		return R.ok( this.removeByIds( ids ) );
	}

	@Override
	@Transactional ( rollbackFor = Exception.class )
	public R < Organization > savaOrUpdate( OrganizationDto dto ) {
		Organization organization = new Organization();
		BeanUtils.copyProperties( dto , organization );
		if ( StringUtils.isEmpty( dto.getId() ) ) {
			organization.setLevel( 1 );
			save( organization );
		}
		else {
			updateById( organization );
		}
		return R.ok( organization );
	}

	@Override
	public R < List < Organization > > getList() {
		List < Organization > organizationList =
				list( new LambdaQueryWrapper < Organization >().eq( Organization::getLevel , 1 ) );
		return R.ok( organizationList );
	}

	@Override
	public R < Organization > detail( String id ) {
		return R.ok( getById( id ) );
	}
}
